package com.ctvit.sjk.knowledge.problem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ctvit.sjk.knowledge.problem.pojo.domain.TabProblem;
import com.ctvit.sjk.knowledge.problem.pojo.domain.TabProblemParagraphMapping;
import com.ctvit.sjk.knowledge.problem.service.ITabProblemParagraphMappingService;
import com.ctvit.sjk.knowledge.problem.service.ITabProblemService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 问题与分段映射Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@RestController
@RequestMapping("knowledge/dataset")
public class TabProblemParagraphMappingController extends KnowledgeController {

    private final ITabProblemService tabProblemService;

    private final ITabProblemParagraphMappingService tabProblemParagraphMappingService;

    public TabProblemParagraphMappingController(ITabProblemService tabProblemService, ITabProblemParagraphMappingService tabProblemParagraphMappingService) {
        this.tabProblemService = tabProblemService;
        this.tabProblemParagraphMappingService = tabProblemParagraphMappingService;
    }

    /**
     * 关联问题与分段映射
     */
    @Log(title = "问题与分段映射", businessType = BusinessType.UPDATE)
    @PutMapping("{datasetId}/document/{documentId}/paragraph/{paragraphId}/problem/{problemId}/association")
    public AjaxResult associationParagraph(@PathVariable String datasetId,
                                           @PathVariable String documentId,
                                           @PathVariable String paragraphId,
                                           @PathVariable String problemId) {
        TabProblemParagraphMapping entity = new TabProblemParagraphMapping();
        entity.setMappingId(IdWorker.getIdStr());
        entity.setDatasetId(datasetId);
        entity.setDocumentId(documentId);
        entity.setParagraphId(paragraphId);
        entity.setProblemId(problemId);
        entity.setCreateId(CREATE_ID);
        entity.setCreateTime(LocalDateTime.now());

        boolean result = tabProblemParagraphMappingService.save(entity);
        if (result) {
            updateProblemParagraphCount(problemId);
        }
        return toAjax(result);
    }

    /**
     * 断开问题与分段映射
     */
    @Log(title = "问题与分段映射", businessType = BusinessType.UPDATE)
    @PutMapping("{datasetId}/document/{documentId}/paragraph/{paragraphId}/problem/{problemId}/un_association")
    public AjaxResult unAssociationParagraph(@PathVariable String datasetId,
                                             @PathVariable String documentId,
                                             @PathVariable String paragraphId,
                                             @PathVariable String problemId) {
        LambdaQueryWrapper<TabProblemParagraphMapping> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TabProblemParagraphMapping::getDatasetId, datasetId);
        wrapper.eq(TabProblemParagraphMapping::getDocumentId, documentId);
        wrapper.eq(TabProblemParagraphMapping::getParagraphId, paragraphId);
        wrapper.eq(TabProblemParagraphMapping::getProblemId, problemId);

        boolean result = tabProblemParagraphMappingService.remove(wrapper);
        if (result) {
            updateProblemParagraphCount(problemId);
        }
        return toAjax(result);
    }

    private void updateProblemParagraphCount(@PathVariable String problemId) {
        LambdaQueryWrapper<TabProblemParagraphMapping> mappingWrapper = new LambdaQueryWrapper<>();
        mappingWrapper.eq(TabProblemParagraphMapping::getProblemId, problemId);
        long count = tabProblemParagraphMappingService.count(mappingWrapper);

        LambdaUpdateWrapper<TabProblem> problemWrapper = new LambdaUpdateWrapper<>();
        problemWrapper.set(TabProblem::getParagraphCount, count);
        problemWrapper.set(TabProblem::getUpdateId, CREATE_ID);
        problemWrapper.set(TabProblem::getUpdateTime, LocalDateTime.now());
        tabProblemService.update(problemWrapper);
    }

}
